<template>
	<view>
		<app-header title="故障排查"></app-header>
		<view class="search flex-c">
			<view class="input flex-s">
				<u-icon name="search" color="#8C8A8A" size="48rpx"></u-icon>
				<input v-model="query.name" placeholder="故障查询" />
				<view class="btn flex-c" @click="search">搜索</view>
			</view>
		</view>
		<view class="tabs flex-s">
			<view class="tabs_son flex-c" :class="[query.type_id==item.id?'active':'']" @click="setActive(item.id)"
				v-for="(item,index) in classify" :key="index">{{item.type_name}}</view>
		</view>

		<scroll-view scroll-y="true" :refresher-triggered="triggered" class="scroll-y" @refresherrefresh="handleRefresh"
			@scrolltolower="lower" :style="{height:scrollHeight}" :enhanced="true" :show-scrollbar="true">
			<view class="device_list">

				<view v-for="(item,index) in list" :key="index">
					<view class="cell" @click="setOpen(index)">
						<view class="label">{{index+1}}、{{item.title}}</view>
						<view class="value">
							<view class="flex-s" style="width: 100%;">

								<view class="icon flex-c" :class="[openIndex==index?'open':'']">
									<u-icon size="32rpx" name="arrow-right" color="#BBBBBB"></u-icon>
								</view>
							</view>

						</view>
					</view>
					<view class="textarea" v-if="openIndex==index">
						<rich-text :nodes="content(item.content)"></rich-text>
					</view>
				</view>
			</view>
			<u-divider text="暂无数据" v-if="list.length==0 && finish"></u-divider>
			<u-divider text="暂无更多数据" v-if="list.length>0 && finish"></u-divider>
			<u-divider text="加载中" v-if="loading"></u-divider>
		</scroll-view>
	</view>
</template>

<script>
	const systemInfo = uni.getSystemInfoSync();
	const safeArea = systemInfo.safeArea;
	export default {
		data() {
			return {
				openIndex: -1,
				active: '0',
				classify: [],
				loading: false,
				triggered: false,
				finish: false,
				total: 0,
				list: [],
				query: {
					status: '0',
					page: 1,
					pageSize: 20
				},
			}
		},
		computed: {
			// classifyR() {
			// 	return this.classify.reverse()
			// },
			scrollHeight() {
				return 'calc(100vh -  200rpx)'
			},
			content() {
				return (content) =>{
					return content.replace(/\<img/gi,
						'<img style="max-width:100%;height:auto;display:block;"');
				}
	 
			},
			
			 
		},
		onLoad(options) {
			this.getInfo()
		},
		methods: {
			search() {
				this.query.page = 1
				this.list = []
				this.finish = false
				this.getList()
			},
			setOpen(index) {
				this.openIndex = this.openIndex == index ? -1 : index
			},
			getInfo() {
				this.$http.post('/data/api.auth.User/newsType', {
					
				}).then(res => {
					this.classify = (res.data || []).reverse()
					this.$set(this.query, 'type_id', this.classify[0].id)
					this.getList()
				}).catch(() => {

				})
			},
			setActive(active) {
				this.$set(this.query, 'type_id', active)
				this.search()
			},
			handleRefresh(e) {
				this.triggered = true;
				setTimeout(() => {
					this.query.page = 1
					this.list = []
					this.getList()
				}, 500)
			},
			lower() {
				if (this.list.length < this.total) {
					this.query.page = this.query.page + 1
					this.getList()
				} else {
					this.finish = true
					this.loading = false
				}
			},
			getList() {
				this.loading = true
				this.$http.post('/data/api.auth.User/newslist', {...this.query}).then(res => {
					this.list = [...this.list, ...res.data.list]
					this.total = res.data.page.total || 0
					this.loading = false
					this.triggered = false;
					this.finish = true
				}).catch(() => {
					this.loading = false
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.icon {
		transition-duration: 0.3s;
		width: 40rpx;
		height: 40rpx;

		&.open {
			transform: rotate(90deg);
		}
	}

	.textarea {
		background-color: #F5F5F5;
		border-radius: 10rpx;
		padding: 20rpx;
	}

	.tabs {
		height: 100rpx;
		background: #FFFFFF;

		.tabs_son {
			font-size: 28rpx;
			color: #ADADAD;
			min-width: 80rpx;
			padding: 0 30rpx;
			height: 100rpx;
			border-bottom: 2rpx solid #FFFFFF;
			position: relative;


			&.active {
				color: #4F6FF6;

				&::after {
					position: absolute;
					content: '';
					background-color: #4F6FF6;
					width: 80rpx;
					height: 4rpx;
					bottom: 0;
				}

				// border-bottom: 2rpx solid #4F6FF6;
			}
		}
	}

	.device_list {
		padding: 20rpx;
	}

	.cell {
		.label {
			color: #333333;
		}
	}

	.search {
		padding-top: 20rpx;
		height: 80rpx;

		.input {
			margin: 0 20rpx;
			padding: 0 10rpx;
			height: 80rpx;
			flex: 1;
			background: #F3F2F2;
			border-radius: 46px;


			input {
				// border: none;
				flex: 1;
				font-size: 14px;
				margin: 0 10rpx;
			}

			.btn {
				width: 156rpx;
				height: 68rpx;
				background: rgba(79, 111, 246, 1);
				border-radius: 34rpx;
				color: #FFFFFF;
				font-size: 30rpx;
			}

		}
	}
</style>